//
//  ForgetViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/9/14.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "ForgetViewController.h"
#import "UIColor+Hex.h"
#import "AFHTTPRequestOperationManager.h"
#import "HttpReauestParam.h"
#import "MBProgressHUD+MJ.h"
#import "Base64Until.h"
@interface ForgetViewController ()

@end

@implementation ForgetViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden = NO;
    
    //    self.navigationController.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"做" style:UIBarButtonItemStylePlain target:self action:@selector()];
    
    
    
    
    
    
    [self.limitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.limitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.limitBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    [self.limitBtn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateSelected];
    [self.limitBtn addTarget:self action:@selector(limitBtn_click) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.codeBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateNormal];
    [self.codeBtn setTitleColor:[UIColor colorWithHexString:@"5ff7e00"] forState:UIControlStateSelected];
    self.codeBtn.layer.borderWidth = 1;
    self.codeBtn.layer.borderColor = [UIColor colorWithHexString:@"#ff7e00"].CGColor;
    self.codeBtn.layer.masksToBounds = YES;
    [self.codeBtn addTarget:self action:@selector(codeBtnClick2) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)codeBtnClick{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    return theImage;
}

-(void)viewWillAppear:(BOOL)animated{
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.timer setFireDate:[NSDate distantFuture]];
    
    self.navigationController.navigationBar.hidden = YES;
}


-(void)codeBtnClick2{
    
    if ([self.field_phone.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入手机号"];
    }else{
        [MBProgressHUD showMessage:@"获取验证码"];
        
        AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
        manage.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSDictionary *dic = @{@"phone":self.field_phone.text};
        
        HttpReauestParam *param = [[HttpReauestParam alloc] initWithData:@"app_system_getVerificationCode" content:[self dictionaryToJson:dic] urlType:0];
        [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject){
            [MBProgressHUD hideHUD];
            
            NSDictionary *data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
            NSLog(@"%@",data);
            if ([data[@"success"] boolValue]) {
                [MBProgressHUD showSuccess:@"获取成功"];
                
                //启动定时
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerDo) userInfo:nil repeats:YES];
                self.count = 59;
                [self.codeBtn setTitle:[NSString stringWithFormat:@"%i秒",self.count] forState:UIControlStateNormal];
                
                [self.codeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [self.codeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
                self.codeBtn.layer.borderWidth = 1;
                self.codeBtn.layer.borderColor = [UIColor grayColor].CGColor;
                self.codeBtn.layer.masksToBounds = YES;
                [self.timer setFireDate:[NSDate distantPast]];
                
                
            }else{
                [MBProgressHUD showError:data[@"errorMsg"]];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD hideHUD];
        }];
    }
    
}

-(void) timerDo{
    
    NSLog(@"tt");
    self.count--;
    self.codeBtn.enabled = NO;
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%i秒",self.count] forState:UIControlStateNormal];//[NSString stringWithFormat:@"%i",self.count];
    if(self.count == 1){
        self.codeBtn.enabled = YES;
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.codeBtn setTitleColor:[UIColor colorWithHexString:@"#ff7e00"] forState:UIControlStateNormal];
        [self.codeBtn setTitleColor:[UIColor colorWithHexString:@"#55ff7e00"] forState:UIControlStateSelected];
        self.codeBtn.layer.borderWidth = 1;
        self.codeBtn.layer.borderColor = [UIColor colorWithHexString:@"#ff7e00"].CGColor;
        self.codeBtn.layer.masksToBounds = YES;
        
        [self.timer setFireDate:[NSDate distantFuture]];
    }
    
    
}
- (IBAction)back_Click:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)touch_view:(id)sender {
    [self.view endEditing:YES];
}

-(NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

- (void)limitBtn_click{
    if ([self.field_phone.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入手机号"];
    }else if ([self.field_code.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入验证码"];
    }else if ([self.field_pwd1.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入密码"];
    }else if ([self.field_pwd2.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请再次输入密码"];
    }else if (![self.field_pwd1.text isEqualToString:self.field_pwd2.text]) {
        [MBProgressHUD showError:@"两次密码输入不一致"];
    }else{
        //找回密码
        
        NSDictionary *dic = @{@"phone":self.field_phone.text,@"code":self.field_code.text,@"password":[Base64Until encode:self.field_pwd1.text]};
        
        HttpReauestParam * param = [[HttpReauestParam alloc]initWithData:@"app_system_RetrievePassword" content:[self dictionaryToJson:dic] urlType:0];
        
        [MBProgressHUD showMessage:@"找回中"];
        AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
        manage.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [MBProgressHUD hideHUD];
            
            NSDictionary *data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
            
            if ([data[@"success"] boolValue]) {
                [MBProgressHUD showSuccess:@"找回成功"];
            }else{
                [MBProgressHUD showError:data[@"errorMsg"]];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络连接失败！"];
        }];
    }
}


@end
